<template>
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form ref="queryFormRef" :inline="true" :model="paramsData" class="-mb-10px" label-width="68px">
        <el-form-item label="单位名称" prop="name">
          <el-input v-model="paramsData.name" class="!w-300px" clearable placeholder="请输入单位名称" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="信用代码" prop="creditCode">
          <el-input v-model="paramsData.creditCode" class="!w-300px" clearable placeholder="请输入社会统一信用代码"
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="paramsData.contactPerson" class="!w-300px" clearable placeholder="请输入联系人"
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="paramsData.phone" class="!w-300px" clearable placeholder="请输入联系电话"
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="单位类型" prop="type">
          <el-select v-model="paramsData.type" class="!w-300px" clearable placeholder="请选择单位类型">
            <el-option v-for="item in unitTypeList" :key="item.value" :label="item.label" :value="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button v-hasPermi="['wise:contract:query']" @click="handleQuery">
            <Icon class="mr-5px" icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon class="mr-5px" icon="ep:refresh" />
            重置
          </el-button>
          <el-button v-hasPermi="['wise:contract:create']" plain type="primary" @click="openForm('create')">
            <Icon class="mr-5px" icon="ep:plus" />
            新增
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="tableData">
        <template v-for="item in tableColumn" :key="item.prop">
          <el-table-column
            :width="item.width"
            :align="item.align ? item.align : 'center'" 
            :label="item.label"
            :prop="item.prop"
          >
            <template #default="scope">
              <div v-if="item.prop == 'createTime'">
                <p>创建时间：{{ scope.row.createTime ? formatDate(scope.row.createTime) : '-' }}</p>
                <p>更新时间：{{ scope.row.updateTime ? formatDate(scope.row.updateTime) : '-' }}</p>
              </div>
              <div v-else-if="item.prop == 'creator'">
                <p>创建：{{ scope.row.creator ? scope.row.creator : '-' }}</p>
                <p>更新：{{ scope.row.updater ? scope.row.updater : '-' }}</p>
              </div>
              <div v-else-if="item.prop == 'status'">
                <el-tag type="warning" v-if="scope.row.status == 0">保存中</el-tag>
                <el-tag type="success" v-else>已提交</el-tag>
              </div>
              <div v-else>
                {{ scope.row[item.prop] || '-' }}
              </div>
            </template>
          </el-table-column>
        </template>
  
        <el-table-column :width="200" align="center" label="操作">
          <template #default="scope">
            <el-button v-hasPermi="['wise:contract:update']" link type="primary" @click="openForm('update', scope.row.id)">
              编辑
            </el-button>
            <el-button v-hasPermi="['wise:contract:delete']" link type="danger" @click="handleDelete(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
  
      </el-table>
  
  
      <!-- 分页 -->
      <Pagination v-model:limit="paramsData.pageSize" v-model:page="paramsData.pageNo" :total="total" @pagination="getList" />
    </ContentWrap>

    <!-- 表单弹窗：添加/修改 -->
    <ContactForm ref="formRef" />
</template>
<script setup>
  import {ref,reactive} from "vue"
  import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  import { formatDate } from '@/utils/formatTime'
  import * as ContactApi from '@/api/viewsApi/contactUnits'
  import ContactForm from './contactForm.vue'
  const message = useMessage() // 消息弹窗
  const { t } = useI18n() // 国际化
  const formRef = ref() // 表单 Ref
  const unitTypeList = ref(getIntDictOptions(DICT_TYPE.UNIT_TYPE)) // 单位类型数据
  const queryFormRef = ref() // 搜索的表单
  const paramsData = reactive({
    name: '', // 单位名称
    creditCode: '', // 单位信用代码
    contactPerson: '', // 联系人
    phone: '', // 联系电话
    type: '', // 单位类型
    pageNum: 1,
    pageSize: 10,
  })
  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const tableData = ref([]) // 列表的数据
  const tableColumn = ref([
    { prop: 'name', label: '单位名称', align: 'center', },
    { prop: 'type', label: '单位类型', align: 'center'},
    { prop: 'creditCode', label: '社会统一信用代码', align: 'center'},
    { prop: 'contactPerson', label: '联系人', align: 'center'},
    { prop: 'phone', label: '联系电话', align: 'center'},
    { prop: 'createTime', label: '时间', align: 'center', width: 240},
    { prop: 'creator', label: '操作人', align: 'center', width: 240},
    { prop: 'status', label: '状态', align: 'center'},
  ]) // 表格的列


  /** 查询角色列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await ContactApi.getContactlList(paramsData)
      tableData.value = data.list
      total.value = data.total
    } finally {
      loading.value = false
    }
  }
  provide('getList', getList)

  /** 搜索按钮操作 */
  const handleQuery = () => {
    paramsData.pageNo = 1
    getList()
  }

  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }

  const openForm = (type, id) => {
    formRef.value.open(type, id)
  }

  /** 删除按钮操作 */
  const handleDelete = async (id) => {
    try {
      // 删除的二次确认
      await message.delConfirm()
      // 发起删除
      await ContactApi.deleteContactl(id)
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
    } catch {}
  }

  /** 初始化 **/
  onMounted(() => {
    getList()
  })
</script>
<style lang="less" scoped>

</style>
